//
//  ViewController.m
//  GCD
//
//  Created by fangyuan ding on 2019/12/19.
//  Copyright © 2019年 fangyuan ding. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong)UIScrollView *scrollView;

@property (nonatomic, strong)UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//    self.scrollView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:self.scrollView];
//    self.imageView = [[UIImageView alloc]init];
//    [self.scrollView addSubview:self.imageView];
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        NSURL *url = [NSURL URLWithString:@"https://pics4.baidu.com/feed/359b033b5bb5c9ea218a766c2dc95d053bf3b33e.jpeg?token=e5d87fc8ccc5bf28ace671b69129f577&s=DC8201D79D539AD2467924A703005001"];
//        NSData *data = [NSData dataWithContentsOfURL:url];
//        UIImage *imge = [UIImage imageWithData:data];
//        //到主线程更新UI
//        dispatch_sync(dispatch_get_main_queue(), ^{
//            self.imageView.image = imge;
//            [self.imageView sizeToFit];
//            self.scrollView.contentSize = imge.size;
//        });
//    });
//    //消息循环
//    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerTest) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
//
//    //开启子线程
//    NSThread *thread  = [[NSThread alloc]initWithTarget:self selector:@selector(timerTest) object:nil];
//    [thread start];
//    //在子线程的消息循环中添加输入源
//    [self performSelector:@selector(threadTest) onThread:thread withObject:nil waitUntilDone:NO];


}
- (void)timerTest{
    NSLog(@"timer %@",[NSRunLoop currentRunLoop].currentMode);
    //开启子线程的消息循环
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
//    [[NSRunLoop  currentRunLoop] run];
    NSLog(@"end");
    
}
// //在子线程的消息循环中
- (void)threadTest{
    NSLog(@"thread %@",[NSRunLoop currentRunLoop].currentMode);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self performSelector:@selector(test) withObject:NULL];
    //创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //创建任务
    dispatch_block_t task = ^{
        NSLog(@"hello %@",[NSThread currentThread]);
    };
    dispatch_async(queue, task);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
          NSLog(@"hello %@",[NSThread currentThread]);
    });
    
}

- (void)test{
    __block int a = 0;
    while (a < 5) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            a++;
        });
        NSLog(@"里面a == %d",a);
    }
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"外面a == %d",a);
    });
}
@end
